#ifndef __ConsoleConnection_h__
#define __ConsoleConnection_h__
#pragma once

#include <INotificationNetwork.h>
#include "CryThread.h"

class CConsoleConnection:
	public CryThread<CConsoleConnection>,
	public INotificationNetworkConnectionCallback,
	public IEditorNotifyListener
{
private:
	struct SConnection
	{
		SConnection():pClient(NULL),
			bFailedToConnect(false),
			bIsConnecting(false),
			boPlatformStatusUpdated(false)
		{};

		INotificationNetworkClient *pClient;
		bool												bFailedToConnect;
		bool												bIsConnecting;
		int													nStatusItemId;

		CString											strPlatformName;
		size_t											nStatusIndex;
		bool												boPlatformStatusUpdated;

		CryMutex										oConnectionMutex;
		CryMutex										oConnectionStatusMutex;
	};
public:
	CConsoleConnection();
	~CConsoleConnection();

	static INotificationNetwork *GetNotificationNetwork();

	// Returns true if the given platform is connected to the network.
	// Remarks:	may cause change the Failed to Connect status.
	bool IsConnected(CHotUpdateSystem::EConsolePlatform ePlatform);

	//// Returns true if the given platform has an instance of CryEngine running.
	//bool IsRunning(CHotUpdateSystem::EConsolePlatform ePlatform);

	//////////////////////////////////////////////////////////////////////////

	//
	void BeginStatusCheck(CHotUpdateSystem::EConsolePlatform ePlatform);

	//
	void EndStatusCheck(CHotUpdateSystem::EConsolePlatform ePlatform);


	//
	bool IsFailedToConnect(CHotUpdateSystem::EConsolePlatform ePlatform);

	//
	bool IsConnecting(CHotUpdateSystem::EConsolePlatform ePlatform);
	//////////////////////////////////////////////////////////////////////////


	// Copies a file to the given destination platform.
	bool FileCopyTo(CHotUpdateSystem::EConsolePlatform ePlatform, const char *localSource, const char *remoteDestination, const char * targetname=NULL);

	// Copies a file from the given source platform.
	//bool FileCopyFrom(CHotUpdateSystem::EConsolePlatform ePlatform, const char *remoteSource, const char *localDestination, const char * targetname=NULL);

	// Launches the game on the console.
	bool Launch(CHotUpdateSystem::EConsolePlatform ePlatform, const char * szProgramFilename,const char* szWorkingDirectory,const char* szParameters, const char * targetname=NULL);

	// The selected console.
	bool Reset(CHotUpdateSystem::EConsolePlatform ePlatform, const char * targetname=NULL);

	// Begin using the Notification Network Client for the given platform.
	// If NULL is returned, it was impossible to establish a connection to the
	// given platform.
	// IMPORTANT: If the method was successful it is important to call
	// NotificationEnd() when done using the Notification Network Client.
	INotificationNetworkClient *NotificationBegin(CHotUpdateSystem::EConsolePlatform ePlatform);

	// End using the Notification Network Client for the given platform.
	void NotificationEnd(CHotUpdateSystem::EConsolePlatform ePlatform);

	void NotificationReset(CHotUpdateSystem::EConsolePlatform ePlatform);

	// Warning: this method is not thread safe. 
	bool Connect(CHotUpdateSystem::EConsolePlatform ePlatform);

	// Thread run method.
	void Run();

	//////////////////////////////////////////////////////////////////////////
	// IEditorNotifyListener
	//////////////////////////////////////////////////////////////////////////
	// We only need to deal with the document events to prevent crashing.
	void OnEditorNotifyEvent( EEditorNotifyEvent event );
	//////////////////////////////////////////////////////////////////////////
private:
	bool RetrieveConnectionAddress(CHotUpdateSystem::EConsolePlatform ePlatform, string &address);

	bool ConCom(CHotUpdateSystem::EConsolePlatform ePlatform, const char *name, const char *parameters, string &result);

	void OnConnect(INotificationNetworkClient* pClient,bool bSucceeded);
	void OnDisconnected(INotificationNetworkClient* pClient);

	// Returns the input parameter.
	CString& GetPlatformStatusString(CHotUpdateSystem::EConsolePlatform ePlatform,CString& strStatus);
private:
	SConnection m_connections[CHotUpdateSystem::eNumberOfPlatforms];

	// Thread parameters...
	CHotUpdateSystem::EConsolePlatform			m_ePlatform;
	string																	m_strParameters;
	CryMutex																m_oConComMutex;
};

#endif //__ConsoleConnection_h__
